package cn.swh.guguanjia.service.impl;

import cn.swh.guguanjia.entity.SysRole;
import cn.swh.guguanjia.mapper.SysRoleMapper;
import cn.swh.guguanjia.service.SysRoleService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class SysRoleServicelmpl extends BaseServicelmpl<SysRole, Long> implements SysRoleService {

    @Autowired
    SysRoleMapper SysRoleMapper;

    @Override
    public PageInfo<SysRole> selectPage(int pageNum, int pageSize, Map<String, Object> map) {
        PageHelper.startPage(pageNum, pageSize);
        List<SysRole> sysUsers = SysRoleMapper.selectPage(map);
        PageInfo<SysRole> pageInfo = new PageInfo<>(sysUsers);
        return pageInfo;
    }

    @Override
    public int deleteBatch(long rid, long[] ids) {
        return SysRoleMapper.deleteBatch(rid, ids);
    }

    @Override
    public int insertBatch(long rid, List<Long> cids) {
        return SysRoleMapper.insertBatch(rid, cids);
    }

    @Override
    public int updateByPrimaryKeySelective(Map<String, Object> map) {
        Object role = map.get("role");
        ObjectMapper objectMapper = new ObjectMapper();
        SysRole sysRole = null;
        try {
            String string = objectMapper.writeValueAsString(role);
            sysRole = objectMapper.readValue(string, SysRole.class);
            sysRole.setUpdateDate(new Date());
            super.updateByPrimaryKeySelective(sysRole);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }

//        resources
        List<Integer> resources = null;
        if (map.containsKey("resources")) {
            resources = (List<Integer>) map.get("resources");
        }
        List<Integer> _resources = null;
        if (map.containsKey("_resources")) {
            _resources = (List<Integer>) map.get("_resources");
        }
        if (resources.size() == _resources.size() && resources.containsAll(_resources)) {

        } else {
            SysRoleMapper.deleteById(sysRole.getId());
            SysRoleMapper.insertRoleResources(sysRole.getId(), resources);
        }

//        offices
        List<Integer> offices = null;
        if (map.containsKey("offices")) {
            offices = (List<Integer>) map.get("offices");
        }
        List<Integer> _offices = null;
        if (map.containsKey("_offices")) {
            _offices = (List<Integer>) map.get("_offices");
        }
        if (offices == null && _offices == null) {
            System.out.println("数据一致");
        } else if (offices != null && _offices != null
                && offices.size() == _offices.size() && offices.containsAll(_offices)) {
            System.out.println("数据一致");
        } else {
            SysRoleMapper.deleteOfficeById(sysRole.getId());
            if (offices.size() > 0) {
                SysRoleMapper.insertRoleOffices(sysRole.getId(), offices);
            }
        }
        return 0;
    }
}
